using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace project
{
    public partial class Payment : Form
    {
        static OleDbConnection  conn;
        static string query;
        static OleDbCommand command;
        String[] mname =new String[100];
        static int total_amount;

        public Payment()
        {
            //calculate total amount to be purchased
            //Display publisher and total amount in form
            InitializeComponent();
            total_amount=0;
            command = new OleDbCommand();
            foreach (DBClass obj in Form3.purchase_list)
            {         
             
                total_amount+=(obj.price*obj.qty);
            }
            textBox1.Text = Form3.purchase_list[0].PNAME;
            textBox2.Text = total_amount.ToString();
            dbconnection();
            update_datagrid();            
            

        }

        private void dbconnection()
        {
            try
            {
                conn = DBComponent.Mag_DealerDB.dbconnection();
                conn.Open();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }
        private void update_datagrid()
        {
            //updates the data grid which displays the purchase details
            try
            {

               

                query = "delete * from temp;";
                
                command.CommandText = query;
                command.Connection = conn;
                command.ExecuteNonQuery();
                int sno = 0;
                foreach (DBClass obj in Form3.purchase_list)
                {
                    sno++;
                    query = "insert into temp(SNO,MNAME,MDATE,MPRICE,QTY,TOTAL) VALUES('"+sno+"','" + obj.MNAME + "','" + obj.MDATE + "','" + obj.price + "','" + obj.qty + "','" + (obj.price*obj.qty) + "');";
                    command.CommandText = query;
                    command.ExecuteNonQuery();
                }
                String s;
                s = "Select * from temp";
                OleDbCommand cmd1 = new OleDbCommand(s, conn);
                OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "temp");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "temp";
                tempBindingSource.ResetBindings(false);
                this.Refresh();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private void Payment_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.temp' table. You can move, or remove it, as needed.
            this.tempTableAdapter.Fill(this.mag_dealerDataSet.temp);

        }

        private void Pay_Button_Click(object sender, EventArgs e)
        {
       
            Pay_Publisher pay = new Pay_Publisher();
            pay.curr_amt = int.Parse(textBox2.Text);
            Program.InvokeModal(this, "project.Pay_Publisher");
        }

        public static void UpdatePublisherDue()
        {
            query = "insert into publisher_due(CURR_DATE,PNAME,TOTAL_AMOUNT_BOUGHT) VALUES('" + DateTime.Now + "','" + Form3.purchase_list[0].PNAME + "','" + total_amount + "');";
            command = new OleDbCommand(query, conn);
            command.ExecuteNonQuery();
        }

        public static void UpdateMagazineBought()
        {
            int sno = 0;
            query = "SELECT TOP 1 * from publisher_due Order By S_NO DESC";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                sno = int.Parse(reader["S_NO"].ToString());
            }


            Console.WriteLine(sno);
            reader.Close();
            Insert q = new Insert("magazine_bought");
            foreach (DBClass obj in Form3.purchase_list)
            {
                query = "insert into magazine_bought(CURR_DATE,MNAME,MDATE,PNAME,QUANTITY,PRICE,TOTAL,S_NO) VALUES('" + DateTime.Now + "','" + obj.MNAME + "','" + obj.MDATE + "','" + obj.PNAME + "','" + obj.qty + "','" + obj.price + "','" + (obj.price * obj.qty) + "','" + sno + "');";                
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }

        private void AddToCreditButton_Click(object sender, EventArgs e)
        {
            SavePublisherReceipt();
            UpdatePublisherDue();
            UpdateMagazineBought();
            UpdatePublisherCredit();
            UpdateOrderMagazine();
            Program.InvokeModal(this, "project.Publisher_Receipt");

        }

        private void SavePublisherReceipt()
        {
            Publisher_Receipt_Object pub_rec = new Publisher_Receipt_Object();
            pub_rec.amount_paid = 0;
            query = "select AMOUNT_DUE from publisher_credit where PNAME='"+textBox1.Text+"';" ;
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            int amt_due = 0;
            while (reader.Read())
            {
                amt_due = int.Parse(reader[0].ToString());
            }
            pub_rec.current_due = amt_due+int.Parse(textBox2.Text) - pub_rec.amount_paid;
            pub_rec.pname = textBox1.Text;
            pub_rec.trans_date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            Form3.pub_receipt = pub_rec;

        }



        private void UpdateOrderMagazine()
        {
            command.Connection = conn;
            foreach (DBClass obj in Form3.purchase_list)
            {
                query = "insert into order_magazine(MNAME,MDATE,PNAME,QTY_ORDERED) VALUES('" + obj.MNAME + "','" + obj.MDATE + "','" + obj.PNAME + "','" + obj.qty +"');";
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }

        private void UpdatePublisherCredit()
        {
            int deposit = 0;
            int amount_due = 0;
            query = "select DEPOSIT,AMOUNT_DUE from publisher_credit where PNAME='" + Form3.purchase_list[0].PNAME+"';";
            command.CommandText = query;
            command.Connection = conn;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                deposit = int.Parse(reader[0].ToString());
                amount_due = int.Parse(reader[1].ToString());
            }
            
            amount_due += total_amount;
            if (amount_due > deposit)
            {
                MessageBox.Show("Warning : Credit limit crossed ");

            }
            reader.Close();
            query = "update publisher_credit set AMOUNT_DUE='" + amount_due + "';";
            command.CommandText = query;
            command.Connection = conn;
            command.ExecuteNonQuery();

        }

        private void Payment_FormClosed(object sender, FormClosedEventArgs e)
        {
            
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }



  
    }
}